package org.wizehack.mri;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.wizehack.mri.io.FileIO;
import org.wizehack.mri.repo.MongoDBAccessPoint;

import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class DocGenerator {
//	private static Logger result;
	private MongoDBAccessPoint dbap;

	public DocGenerator(String projectName){
		File file = new File("./out/out.csv");
		file.delete();
//		PropertyConfigurator.configure("./config/log4j.properties");
//		result = Logger.getLogger(DocGenerator.class);
		dbap = MongoDBAccessPoint.getInstance();
		dbap.useDBName(projectName);

	}
	/**
	 * write a document to report the result of fault localization. <br>
	 * <number>; <file name>; <line number>; <code>; <test suite>; <suspiciousness>
	 * @param file file name to export
	 * @param list the list of all lines to export
	 */
	public void exportToTextDoc(String filePath){
		int number = 0;
		String id;
		String fieName;
		String line;
		String code;
		String testSuite;
		String suspiciousness;
		char seperator = '\t';
		String columns = "id" +seperator+ "file" +seperator+ "test suite"+seperator+"line"+seperator+
				"suspciciousness"+seperator+"code" ;
		DBCursor cursor = dbap.getDocumentsFromSuspiciousnes();
		System.out.print("export to file...");
		List<String> result = new ArrayList<String>();
		FileIO fileIO = new FileIO();
		fileIO.setOutFile(filePath);
		result.add(columns);
//		result.info(columns);
		try{
			while(cursor.hasNext()){
				DBObject dObj = cursor.next();
				number = number+1;
				id = number+"";
				fieName = dObj.get("file_name").toString();
				line = dObj.get("number").toString();
				code = dObj.get("code").toString().replace('\t', '\0');
				testSuite = dObj.get("test_suite").toString();
				suspiciousness = dObj.get("suspiciousness").toString();
				String doc = id + seperator + 
						fieName + seperator +
						testSuite + seperator +
						line + seperator +
						suspiciousness + seperator +
						code;
				result.add(doc);
//				result.info(doc);				
			}
			
		} finally {
			fileIO.write(result);
			cursor.close();
			dbap.close();
		}
		System.out.print("ok");

	}
	
	/*
	public static void main(String[] args){
		DocGenerator doc = new DocGenerator("GraphEditor");
		String reportFIle = "/home/hsyoun/workspace/jgraphx/Report/analysis.csv";
		doc.exportToTextDoc(reportFIle);
	}
	*/
}
